package com.itheima.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class MyBatisUtil {
    private static SqlSessionFactory sqlSessionFactory=null;
    static {
        try {
            InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");

            //1-2 创建SqlSessionFactory对象
             sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static SqlSession getSqlSession(){
        //1-3 获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
    public static void voidcommitAndClose(SqlSession sqlSession){
        //1-5 提交事务
        sqlSession.commit();

        //1-6 释放资源
        sqlSession.close();
    }
}
